package com.wangzha.common.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.jfinal.kit.PropKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


public class OSSProvider {
    private Logger logger = LoggerFactory.getLogger(OSSProvider.class);
    private OSS ossClient = new OSSClientBuilder()
            .build("http://" + PropKit.get("ali.oss.endpoint"), PropKit.get("ali.accessKeyId"), PropKit.get("ali.accessSecret"));

    /**
     * 通过bucket与fileKey获取URL
     *
     * @param bucket
     * @param fileKey
     * @return
     */
    /*public String urlFromFileKey(String bucket, String fileKey) {
        return "http://" + bucket + "." + this.endpoint + "/" + fileKey;
    }*/

    /**
     * 上传本地文件
     *
     * @param filePath 本地文件路径
     * @param fileKey  指定OSS文件名，传空时自动生成UUID
     * @return
     */
    public String putObject(String filePath, String fileKey) {
        if (fileKey == null) {
            fileKey = UUID.randomUUID().toString().replaceAll("-", "");
        }

        ossClient.putObject(PropKit.get("ali.oss.bucket"), fileKey, new File(filePath));

        logger.info("OSS putObject success! fileKey={}", fileKey);
        return getUrl(fileKey);
    }

    /**
     * 上传文件
     * @param file 文件
     * @param fileKey  指定OSS文件名，传空时自动生成UUID
     * @return
     */
    public String putObject(File file, String fileKey) {
        if (fileKey == null) {
            fileKey = UUID.randomUUID().toString().replaceAll("-", "");
        }

        ossClient.putObject(PropKit.get("ali.oss.bucket"), fileKey, file);

        logger.info("OSS putObject success! fileKey={}", fileKey);
        return getUrl(fileKey);
    }

    /**
     * 下载文件到本地目录
     *
     * @param bucket    Bucket
     * @param fileKey   OSS文件名
     * @param localPath 本地文件目录
     * @return
     */
    public String getObject(String bucket, String fileKey, String localPath) {
        String localFileKey = localPath + fileKey;
        ossClient.getObject(new GetObjectRequest(bucket, fileKey), new File(localFileKey));

        logger.info("OSS getObject success! localFileKey={}", localFileKey);
        return localFileKey;
    }


    /**
     * 检查文件是否存在
     *
     * @param bucket  Bucket
     * @param fileKey OSS文件名
     * @return
     */
    public boolean checkObject(String bucket, String fileKey) {
        return ossClient.doesObjectExist(bucket, fileKey);
    }


    /**
     * 列出所有文件
     *
     * @param bucket    Bucket
     * @param keyPrifix 文件名前缀
     */
    public List<String> listObjects(String bucket, String keyPrifix) {
        List<String> fileList = new ArrayList<>();

        ObjectListing objectListing = ossClient.listObjects(bucket, keyPrifix);
        List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
        for (OSSObjectSummary s : sums) {
            fileList.add(bucket);
        }

        return fileList;
    }

    /**
     * 删除文件
     *
     * @param bucket
     * @param fileKey
     */
    public void deleteObject(String bucket, String fileKey) {
        ossClient.deleteObject(bucket, fileKey);
    }


    /**
     * 获得url链接
     * @return
     */
    public String getUrl(String fileKey) {
        // 设置URL过期时间为10年  3600l* 1000*24*365*10
        //Date expiration = new Date(new Date().getTime() + 60 * 60 * 24);
        Date expiration = new Date(System.currentTimeMillis() + (3600L* 1000 * 24 * 365 * 10));
        // 生成URL
        URL url = ossClient.generatePresignedUrl(PropKit.get("ali.oss.bucket"), fileKey, expiration);
        if (url != null) {
            return url.toString();
        }
        return null;
    }
}
